package com.outstanding.his.medicine.facade;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.medicine.domain.Medicine;
import com.outstanding.his.medicine.service.IMedicineService;
import com.outstanding.his.medicine.service.impl.MedicineService;

@Controller
@SessionAttributes("medicine")
public class MedicineController {

	private IMedicineService medicineService;

	public void setMedicineService(IMedicineService medicineService) {
		this.medicineService = medicineService;
	}

	@RequestMapping("/medicine/new.do")
	public String medicineNewForm(Model model) {
		Medicine medicine = new Medicine();
		model.addAttribute("medicine", medicine);
		return "/medicine/new";
	}

	@RequestMapping(value = "/medicine/submit", method = RequestMethod.POST)
	public String medicineSubmit(@ModelAttribute("medicine") Medicine medicine,
			Model model) {
		if (medicine.getMedicineId() != null) {
			medicine = medicineService.updateMedicine(medicine);
		} else {
			medicine = medicineService.submitMedicine(medicine);
		}
		return "redirect:/medicine/list.do";
	}

	@RequestMapping(value = "/medicine/list.do")
	public String medicineList(Model model, HttpServletRequest request) {
		String medicineCategory = request.getParameter("medicineCategory");
		Collection<Medicine> medicines = null;
		if (medicineCategory == null || medicineCategory.equals("")) {
			medicines = medicineService.getMedicineList();
		} else if (medicineCategory.equals(MedicineService.WESTERN_MEDICINE
				.toString())) {
			medicines = medicineService.getWesternMedicineList();
		} else if (medicineCategory.equals(MedicineService.CHINESE_MEDICINE
				.toString())) {
			medicines = medicineService.getChineseMedicineList();
		} else if (medicineCategory.equals(MedicineService.INSULIN_MEDICINE
				.toString())) {
			medicines = medicineService.getInsulinMedicineList();
		}
		model.addAttribute("medicines", medicines);
		return "/medicine/list";
	}

	@RequestMapping(value = "/medicine/view.do")
	public String medicineView(@RequestParam("medicineId") Integer medicineId,
			Model model) {
		Medicine medicine = medicineService.getMedicine(medicineId);
		model.addAttribute(medicine);
		return "/medicine/view";
	}

	@RequestMapping(value = "/medicine/edit.do")
	public String medicineEdit(@RequestParam("medicineId") Integer medicineId,
			Model model) {
		Medicine medicine = medicineService.getMedicine(medicineId);
		model.addAttribute(medicine);
		return "/medicine/edit";
	}

}
